package com.qf.service.impl;

import com.qf.dao.RegistrationDao;
import com.qf.dao.impl.RegistrationDaoImpl;
import com.qf.entity.Registration;
import com.qf.entity.RegistrationVo;
import com.qf.service.RegistrationService;
import com.qf.utils.PageUtils;

import java.sql.SQLException;
import java.util.List;

public class RegistrationServiceImpl implements RegistrationService {
    private RegistrationDao registrationDao = new RegistrationDaoImpl();
    @Override
    public PageUtils<Registration> selectByPage(RegistrationVo registrationVo, String currentPageStr, String pageSizeStr) {
        //对当前页面进行处理
        Integer currentPageNo = null;
        if (currentPageStr == null || "".equals(currentPageStr)) {
             currentPageNo = 1;
        }else {
            currentPageNo = Integer.parseInt(currentPageStr);
        }

        //对页量进行处理
        Integer pageSize = null;
        if (pageSizeStr == null || "".equals(pageSizeStr)) {
            pageSize = 4;
        }else {
            pageSize = Integer.parseInt(pageSizeStr);
        }

        Integer totalPageCount = null;
        Integer totalPageSize= null;
        List<Registration> registrationList = null;
        try {
            //查询总记录数
            totalPageCount = registrationDao.selectCountRegistration(registrationVo);
            //根据总记录数计算出总页数
            totalPageSize = totalPageCount % pageSize == 0 ? totalPageCount/pageSize:totalPageCount/pageSize+1;
            //查询出当前页的集合
             registrationList = registrationDao.selectAllRegistration(registrationVo, currentPageNo, pageSize);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

        //实例化分页工具类对象
        PageUtils<Registration> pageUtils = new PageUtils<>();
        pageUtils.setCurrentPageNo(currentPageNo);
        pageUtils.setPageSize(pageSize);
        pageUtils.setTotalPageCount(totalPageCount);
        pageUtils.setTotalPageSize(totalPageSize);
        pageUtils.setList(registrationList);
        return pageUtils;
    }
}
